package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.MPayServiceMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Channel;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.PayService;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.PayServiceDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchPayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IPayServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PayServiceServiceImpl implements IPayServiceService {

    @Autowired
    private PayServiceDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    MPayServiceMapper mapper;

    @Override
    public void deleteChannel(Integer deptId) {
        PayService channel = mapper.selectById(deptId);
        Wrapper<PayService> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + channel.getId() + "]%");
        List<PayService> subChannels = mapper.selectList(wrapper);
        for (PayService temp : subChannels) {
            temp.deleteById();
        }
        channel.deleteById();
    }

    public PayServiceInfo selectOneById(String payServiceId) {
        String sql = "select * from pay_service_info where id= '" + payServiceId + "'";
        PayServiceInfo info = sqlMapper.selectOne(sql, PayServiceInfo.class);
        return info;
    }

    public List<PayServiceInfo> selectListByDepartment(String department){
        String sql = "select id as id,department as department, first_level_project as firstLevelProject from pay_service_info a where department= '"+department+"'" +
                "group by first_level_project order by id";
        List<PayServiceInfo> payServiceInfoList = sqlMapper.selectList(sql, PayServiceInfo.class);
        return payServiceInfoList;
    }

    public List<PayServiceInfo> selectListByFirstLevelProject(String department,String firstLevelProject){
        String sql = "select id as id ,num as num ,department as department ," +
                " first_level_project as firstLevelProject,pay_service_name as payServiceName," +
                " pay_service_price_one as payServicePriceOne,pay_service_price_two as payServicePriceTwo ," +
                " unit as unit ,remarks as remarks from pay_service_info a where department='"+department+"' " +
                " and first_level_project= '"+firstLevelProject+"'";
        List<PayServiceInfo> payServiceInfoList = sqlMapper.selectList(sql, PayServiceInfo.class);
        return payServiceInfoList;
    }

    public int insert(PayServiceInfo info) {
        String sql = "INSERT INTO `pay_service_info` (`num`,`department`, `first_level_project`, `pay_service_name`, `pay_service_price_one`, `pay_service_price_two`, `unit`, `remarks`) VALUES" +
                "(  " + info.getNum() + " ,'" + info.getDepartment().trim() + "','" + info.getFirstLevelProject() + "'" +
                ",'" + info.getPayServiceName()+ "'," + info.getPayServicePriceOne()+ "," + info.getPayServicePriceTwo()+ "," +
                "'" + info.getUnit()+ "','" + info.getRemarks()+ "');";
        return sqlMapper.insert(sql);
    }

    public int updatePayService(PayServiceInfo info) {
        String sql ="update pay_service_info a set a.department='"+info.getDepartment().trim()+"',a.num="+info.getNum()+",a.first_level_project='"+info.getFirstLevelProject()+"',a.pay_service_name='"+info.getPayServiceName()+"'" +
                ",a.pay_service_price_one="+info.getPayServicePriceOne()+",a.pay_service_price_two="+info.getPayServicePriceTwo()+",a.unit='"+info.getUnit()+"',a.remarks='"+info.getRemarks()+"' where id='"+info.getId()+"'";
        return sqlMapper.update(sql);
    }


    public int addPayUpdate(PayServiceInfo info) {
        String id = CommUtils.getUUID();
        if(info.getPayServiceNum()>0){
            String sql = "INSERT INTO `open_bill_info` ( `is_deal`, `tc_name`,`pay_service_num`, `customer_id`,`reservations_id`, `pay_service_id`, `folding_pay`, `operation_time`, `pay_service`, `left_up`, `left_down`, `right_up`, `right_down`,`doctor`,`nurse`,`service_actual_pay`,`type_number`) VALUES" +
                    "( " + 3 + ",'" + ShiroKit.getUser().getAccount() + "'" +
                    "," + info.getPayServiceNum()+ ",'" + ShiroKit.getSessionAttr("customerId") + "'," +
                    "'" + ShiroKit.getSessionAttr("reservationsId") + "','" + info.getPayServiceId() + "'," + info.getTotalAmount()+ "," +
                    "'" + DateTimeKit.now() + "',"+info.getPayService()+ ",'"+info.getLeftUp()+ "','"+info.getLeftDown()+ "','"+info.getRightUp()+ "','"+info.getRightDown()+"','"+info.getDoctor()+"','"+info.getNurse()+"',"+info.getServiceActualPay()+",'"+info.getTypeNumber()+"');";
            return sqlMapper.insert(sql);
        }else{
            return 0;
        }
    }

    public int addPay(PayServiceInfo info) {
        String id = CommUtils.getUUID();
        if(info.getPayServiceNum()>0){
            String sql = "INSERT INTO `open_bill_info` (`is_deal`, `tc_name`,`pay_service_num`,referraler, `customer_id`,`reservations_id`, `pay_service_id`, `folding_pay`, `operation_time`, `pay_service`, `left_up`, `left_down`, `right_up`, `right_down`,`doctor`,`nurse`,`service_actual_pay`,`type_number`) VALUES" +
                    "(" + 3 + ",'" + ShiroKit.getUser().getAccount() + "'" +
                    "," + info.getPayServiceNum()+ ",'"+info.getReferraler()+"','" + ShiroKit.getSessionAttr("customerId") + "'," +
                    "'" + ShiroKit.getSessionAttr("reservationsId") + "','" + info.getId() + "'," + info.getTotalAmount()+ "," +
                    "'" + DateTimeKit.now() + "',"+info.getPayService()+ ",'"+info.getLeftUp()+ "','"+info.getLeftDown()+ "','"+info.getRightUp()+ "','"+info.getRightDown()+"','"+info.getDoctor()+"','"+info.getNurse()+"',"+info.getServiceActualPay()+",'"+info.getTypeNumber()+"');";
            return sqlMapper.insert(sql);
        }else{
            return 0;
        }
    }

    public PayServiceInfo selectPayService(PayServiceInfo info) {
        String sql = "select * from pay_service_info where id='"+info.getId()+"'";
        PayServiceInfo payServiceInfo= sqlMapper.selectOne(sql,PayServiceInfo.class);
        return payServiceInfo;
    }

    public PayServiceInfo selectPayServices(PayServiceInfo info) {
        String sql = "select * from pay_service_info where id='"+info.getPayServiceId()+"'";
        PayServiceInfo payServiceInfo= sqlMapper.selectOne(sql,PayServiceInfo.class);
        return payServiceInfo;
    }

    @Transactional
    public void delete(String id) {
        String sql = "delete from pay_service_info  where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    @Override
    public List<PayServiceInfo> selectPageListByQuery(Page<PayServiceInfo> page, SearchPayServiceInfo searchInfo){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getPayServiceName())) {
            params.put("payServiceName", searchInfo.getPayServiceName());
        }
        return dao.getQueryPage(page, params);
    }

    public int update(PayServiceInfo info) {
        return dao.updateById(info);
    }

    public List<Map<String, Object>> list() {
        String sql = "select a.department as department from pay_service_info a group by a.department;";
        return sqlMapper.selectList(sql);
    }
}
